package com.emergency.db.service.impl.staff;

import com.emergency.common.util.IdWorker;
import com.emergency.db.dao.staff.StaffDetailDao;
import com.emergency.db.domain.staff.Staff;
import com.emergency.db.domain.staff.StaffDetail;
import com.emergency.db.service.staff.StaffDetailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import javax.persistence.criteria.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
public class StaffDetailServiceImpl implements StaffDetailService {
    @Autowired
    private StaffDetailDao staffDetailDao;

    @Autowired
    IdWorker idWorker;

    //保存
    public void save(StaffDetail staffDetail) {
        staffDetail.setId(idWorker.nextId());
        staffDetailDao.save(staffDetail);
    }

    //查询单项
    public StaffDetail findByStaff(Long id) {
        return staffDetailDao.findByStaff(id);
    }

    //删除
    public void delete(Long id) {
        staffDetailDao.deleteById(id);
    }

    //分页查询
    @Override
    public Page<StaffDetail> findList(Map searchMap, int page, int size) {
        Specification<StaffDetail> specification = createSpecification(searchMap);
        Sort sort = Sort.by(Sort.Direction.ASC, "id");
        PageRequest pageRequest = PageRequest.of(page - 1, size, sort);
        return staffDetailDao.findAll(specification, pageRequest);
    }

    /**
     * 动态条件构建
     *
     * @param searchMap
     * @return
     */
    private Specification<StaffDetail> createSpecification(Map searchMap) {
        return new Specification<StaffDetail>() {

            @Override
            public Predicate toPredicate(Root<StaffDetail> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) {
                List<Predicate> predicateList = new ArrayList<Predicate>();

                //员工状态
                if (searchMap.get("status") != null && !"".equals(searchMap.get("status"))) {

                    predicateList.add(criteriaBuilder.equal(root.get("status").as(String.class), (String) searchMap.get("status")));
                }
                //地址
                if (searchMap.get("address") != null && !"".equals(searchMap.get("address"))) {
                    Join<StaffDetail, Staff> join = root.join("staff", JoinType.LEFT);
                    predicateList.add(criteriaBuilder.like(join.get("address").as(String.class), "%" + (String) searchMap.get("address") + "%"));
                }
                //特殊技能
                if (searchMap.get("skill") != null && !"".equals(searchMap.get("skill"))) {
                    predicateList.add(criteriaBuilder.like(root.get("skill").as(String.class), "%" + (String) searchMap.get("skill") + "%"));
                }
                return criteriaBuilder.and(predicateList.toArray(new Predicate[predicateList.size()]));
            }
        };
    }
}
